<?php
include_once("Constants.php");
include_once("Entity/customer.php");

class customer_dao_base {

	function delete_customer($id)
	{
		try
		{
			$dbh = new PDO(Constants::$connectionString, Constants::$username, Constants::$password);
			$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$dbh->beginTransaction();

			$stmt = $dbh->prepare("DELETE FROM customer WHERE customer_id = :customer_id");
			$stmt->bindParam(':customer_id', $id, PDO::PARAM_INT);
			$stmt->execute();
			
			$dbh->commit();
			$dbh = null;
		}
		catch(PDOException $e)
		{
			echo '<br />' . $e->getMessage();
			$dbh->rollback();
		}	
	}

	function get_customer($id)
	{
		try
		{
			$dbh = new PDO(Constants::$connectionString, Constants::$username, Constants::$password);
			$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$dbh->beginTransaction();
			
			$stmt = $dbh->prepare("SELECT * FROM  customer c WHERE c.customer_id = :customer_id");
			$stmt->bindParam(':customer_id', $id, PDO::PARAM_INT);
			$stmt->execute();
			
			$c = null;
			if($row = $stmt->fetch())
			{
				$c = new customer($row["customer_id"],$row["name"],$row["address"],$row["phone"],$row["email"],$row["fax"]);
			}
			
			$dbh->commit();
			$dbh = null;
			return $c;
		}
		catch(PDOException $e)
		{
			echo '<br />' . $e->getMessage();
			$dbh->rollback();
		}	
	}
	
	function get_customer_list()
	{
		try
		{
			$dbh = new PDO(Constants::$connectionString, Constants::$username, Constants::$password);
			$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$dbh->beginTransaction();

			$aoc = array();
			$stmt = $dbh->query("SELECT * FROM customer");
			$rows = $stmt->fetchAll();
			foreach($rows as $row)
			{
				$c = new customer($row["customer_id"],$row["name"],$row["address"],$row["phone"],$row["email"],$row["fax"]);
				$aoc[] = $c;
			}

			$dbh->commit();
			$dbh = null;
			
			return $aoc;
		}
		catch(PDOException $e)
		{
			echo '<br />' . $e->getMessage();
			$dbh->rollback();
		}	
	}

	function insert_customer($c)
	{
		try
		{
			$dbh = new PDO(Constants::$connectionString, Constants::$username, Constants::$password);
			$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$dbh->beginTransaction();

			$stmt = $dbh->prepare("INSERT INTO customer (name, address, phone, email, fax)
							VALUE(:name ,:address ,:phone ,:email ,:fax)");
							
							
			$stmt->bindParam(':name', $c->name);
			$stmt->bindParam(':address', $c->address);
			$stmt->bindParam(':phone', $c->phone);
			$stmt->bindParam(':email', $c->email);
			$stmt->bindParam(':fax', $c->fax);
			
			$stmt->execute();
			$id = $dbh->lastInsertId();	
			$dbh->commit();
			$dbh = null;

			return $id;
		}
		catch(PDOException $e)
		{
			echo '<br />' . $e->getMessage();
			$dbh->rollback();
		}	
	}

	function update_customer($c)
	{
		try
		{
			$dbh = new PDO(Constants::$connectionString, Constants::$username, Constants::$password);
			$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$dbh->beginTransaction();

			$stmt = $dbh->prepare("UPDATE customer c 
						SET 
						c.name = :name,
						c.address = :address,
						c.phone = :phone,
						c.email = :email,
						c.fax = :fax
						WHERE 
						c.customer_id = :customer_id");
			$stmt->bindParam(':name', $c->name);
			$stmt->bindParam(':address', $c->address);
			$stmt->bindParam(':phone', $c->phone);
			$stmt->bindParam(':email', $c->email);
			$stmt->bindParam(':fax', $c->fax);
			$stmt->bindParam(':customer_id', $c->customer_id);
			
			$stmt->execute();
			
			$dbh->commit();
			$dbh = null;
		}
		catch(PDOException $e)
		{
			echo '<br />' . $e->getMessage();
			$dbh->rollback();
		}	
	}

}
?>